package org.easy.core.util;

import org.easy.core.enums.PayPlatform;
import org.springframework.lang.Nullable;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

//请求原文
// ##  苹果的微信扫一扫
//=== MimeHeaders ===
//host = 47.99.33.166:8888
//upgrade-insecure-requests = 1
//accept = text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
//user-agent = Mozilla/5.0 (iPhone; CPU iPhone OS 17_6_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.53(0x18003531) NetType/WIFI Language/zh_CN
//accept-language = zh-CN,zh-Hans;q=0.9
//accept-encoding = gzip, deflate
//connection = keep-alive
//
//
//
//## 苹果的支付宝扫一扫
//
//=== MimeHeaders ===
//accept = text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
//sec-fetch-mode = navigate
//accept-encoding = gzip
//ts = 1731921422875
//sec-fetch-dest = document
//sec-fetch-site = none
//upgrade-insecure-requests = 1
//sign = bee9afb425be7a70a0265978b145620a
//user-agent = Mozilla/5.0 (iPhone; CPU iPhone OS 17_6_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/21G93 Ariver/1.1.0 AliApp(AP/10.6.60.6000) Nebula WK RVKType(1) AlipayDefined(nt:WIFI,ws:414|832|3.0) AlipayClient/10.6.60.6000 Alipay Language/zh-Hans Region/CN NebulaX/1.0.0 DTN/2.0
//connection = Keep-Alive
//host = 47.99.33.166:8888
//
//## 鸿盟微信扫一扫
//
//=== MimeHeaders ===
//host = 47.99.33.166:8888
//connection = keep-alive
//upgrade-insecure-requests = 1
//user-agent = Mozilla/5.0 (Linux; Android 12; OCE-AN10 Build/HUAWEIOCE-AN10; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/130.0.6723.73 Mobile Safari/537.36 XWEB/1300057 MMWEBSDK/20240802 MMWEBID/2898 MicroMessenger/8.0.53.2740(0x2800353F) WeChat/arm64 Weixin NetType/WIFI Language/zh_CN ABI/arm64
//accept = text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/wxpic,image/tpg,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7
//x-requested-with = com.tencent.mm
//accept-encoding = gzip, deflate
//accept-language = zh-CN,zh;q=0.9,en-CN;q=0.8,en-US;q=0.7,en;q=0.6
//
//## 鸿蒙支付宝
//
//=== MimeHeaders ===
//accept = text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
//x-requested-with = com.eg.android.AlipayGphone
//upgrade-insecure-requests = 1
//x-mass-tappid = 20000067
//accept-encoding = gzip
//accept-language = zh-CN,zh;q=0.9,en-CN;q=0.8,en-US;q=0.7,en;q=0.6
//user-agent = Mozilla/5.0 (Linux; Android 12; OCE-AN10 Build/HUAWEIOCE-AN10; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/105.0.5195.148 MYWeb/0.11.0.241023201746 UWS/3.22.2.9999 UCBS/3.22.2.9999_220000000000 Mobile Safari/537.36 NebulaSDK/1.8.100112 Nebula AlipayDefined(nt:WIFI,ws:424|0|2.55) AliApp(AP/10.6.63.8000) AlipayClient/10.6.63.8000 Language/zh-Hans useStatusBar/true isConcaveScreen/true Region/CNAriver/1.0.0 DTN/2.0
//connection = Keep-Alive
//host = 47.99.33.166:8888
//
//## 小米微信
//
//=== MimeHeaders ===
//host = 47.99.33.166:8888
//connection = keep-alive
//upgrade-insecure-requests = 1
//user-agent = Mozilla/5.0 (Linux; Android 14; 23127PN0CC Build/UKQ1.230804.001; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/126.0.6478.188 Mobile Safari/537.36 XWEB/1260213 MMWEBSDK/20240802 MMWEBID/7333 MicroMessenger/8.0.53.2740(0x2800353F) WeChat/arm64 Weixin NetType/WIFI Language/zh_CN ABI/arm64
//accept = text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/wxpic,image/tpg,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7
//x-requested-with = com.tencent.mm
//accept-encoding = gzip, deflate
//accept-language = zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7
//
//
//
//## 小米支付宝
//=== MimeHeaders ===
//accept = text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
//x-requested-with = com.eg.android.AlipayGphone
//upgrade-insecure-requests = 1
//x-mass-tappid = 20000067
//accept-encoding = gzip
//accept-language = zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7
//user-agent = Mozilla/5.0 (Linux; Android 14; 23127PN0CC Build/UKQ1.230804.001; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/105.0.5195.148 MYWeb/0.11.0.241023201746 UWS/3.22.2.9999 UCBS/3.22.2.9999_220000000000 Mobile Safari/537.36 NebulaSDK/1.8.100112 Nebula AlipayDefined(nt:WIFI,ws:400|0|3.0) AliApp(AP/10.6.63.8000) AlipayClient/10.6.63.8000 Language/zh-Hans useStatusBar/true isConcaveScreen/true Region/CNAriver/1.0.0 DTN/2.0
//connection = Keep-Alive
//host = 47.99.33.166:8888
public class PayPlatformUtil {

    /**
     * 获取匹配的支付平台
     *
     * @param request
     * @return 未匹配平台时返回空
     */
    @Nullable
    public static PayPlatform payPlatformMatch(HttpServletRequest request) {

        List<String> headers = new ArrayList<>();
        headers.add(request.getHeader("origin-x-requested-with"));
        headers.add(request.getHeader("origin-user-agent"));
        headers.add(request.getHeader("x-requested-with"));
        headers.add(request.getHeader("user-agent"));

        headers.removeIf(Objects::isNull);

        headers = headers.stream().map(String::toLowerCase).collect(Collectors.toList());

        for (String header : headers) {
            PayPlatform match = match(header);
            if (match != null) {
                return match;
            }
        }

        return null;
    }

    private static PayPlatform match(String header) {
        if (header.contains("alipay")) {
            return PayPlatform.ALIPAY;
        }
        if (header.contains("com.tencent.mm")) {
            return PayPlatform.ALIPAY;
        }
        return null;
    }
}
